import CryptoJS from 'crypto-js'
import { Message } from 'element-ui'
import { FORMDATA_KEY } from '@/constants/localKey'

export function savePassword(password) {
  const encryptedPassword = CryptoJS.AES.encrypt(password, 'my_secret_key').toString()
  localStorage.setItem(FORMDATA_KEY, encryptedPassword)
}

export function checkPassword(password) {
  const storedPassword = localStorage.getItem(FORMDATA_KEY)
  if (storedPassword) {
    const bytes = CryptoJS.AES.decrypt(storedPassword, 'my_secret_key')
    const storedPasswordDecrypted = bytes.toString(CryptoJS.enc.Utf8)
    if (storedPasswordDecrypted === password) {
      // 验证成功，继续操作
      return storedPasswordDecrypted
    } else {
      Message.error('密码错误')
    }
  }
}
